package cn.emernet.demo1.util;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * @author hejx
 */
public class Kv extends HashMap {

    private HttpServletRequest request;

    public Kv() {
    }

    public Kv(HttpServletRequest request) {
        if (request == null) return;
        Iterator entries = request.getParameterMap().entrySet().iterator();
        Entry entry;
        String name = "";
        while (entries.hasNext()) {
            String value = "";
            entry = (Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value += values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            try {
                value = URLEncoder.encode(value, "UTF-8");
                this.put(name, URLDecoder.decode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.request = request;
    }

    public Kv(String key, Object value) {
        this.put(key, value);
    }

    public Kv(String json) {
        super(JSONObject.parseObject(json, Kv.class));
    }

    public Kv append(String key, Object value) {
        if (value == null) {
            value = "";
        }
        this.put(key, value);
        return this;
    }

    public Kv appendNotNull(String key, Object value) {
        if (value != null) {
            this.put(key, value);
        }
        return this;
    }

    public String getString(String key) {
        if (get(key) == null) return "";
        return get(key).toString();
    }

    /**
     * 获取整型值 如果值不存在 那么返回 null
     *
     * @param key
     * @return Integer
     */
    public Integer getInteger(String key) {
        String value = getString(key);
        if (value.equals("")) return null;
        if (value.contains(".")) return (int) Double.parseDouble(value);
        return Integer.parseInt(value);
    }

    /**
     * 获取整型值 如果值不存在 那么返回0
     *
     * @param key
     * @return Integer
     */
    public int getIntVal(String key) {
        return getInteger(key, 0);
    }

    /**
     * 获取整型值
     *
     * @param key
     * @param dv  缺省值
     * @return
     */
    public Integer getInteger(String key, Integer dv) {
        String value = getString(key);
        if (value.equals("")) return dv;
        return getInteger(key);
    }

    public Long getLong(String key) {
        String value = getString(key);
        if (value.equals("")) return null;
        return Long.parseLong(value);
    }

    public long getLongVal(String key) {
        return getLong(key, 0L);
    }

    public Long getLong(String key, long defaultValue) {
        String value = getString(key);
        if (value.equals("")) return defaultValue;
        return getLong(key);
    }

    /**
     * 获取Double值
     *
     * @param key
     * @return Double
     */
    public Double getDouble(String key) {
        String value = getString(key);
        if (value.equals("")) return null;
        return Double.parseDouble(value);
    }

    public double getDoubleVal(String key) {
        return getDouble(key, 0d);
    }

    public Double getDouble(String key, Double defaultValue) {
        String value = getString(key);
        if (value.equals("")) return defaultValue;
        return Double.parseDouble(value);
    }

    public JSONObject getJSONObject(String key) {
        String val = getString(key);
        if ("".equals(val)) return null;
        return JSONObject.parseObject(val);
    }

    /**
     * 转换成字符串格式
     * "name=王五&age=6..."
     *
     * @return
     */
    public String serialize() {
        Set<String> keys = this.keySet();
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (String key : keys) {
            if (!isFirst) sb.append("&");
            sb.append(key).append("=").append(this.getString(key));
            if (isFirst) {
                isFirst = false;
            }
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
